package ncov.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.EasyExcel;
import ncov.dao.DepartmentDao;
import ncov.dao.ReportRecordDao;
import ncov.dao.StudentDao;
import ncov.model.Department;
import ncov.model.Grade;
import ncov.model.ReportRecord;
import ncov.model.Student;
import ncov.service.ReportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author WuChangJian
 * @date 2020/5/31 8:57
 */
@Service
@Transactional
public class ReportRecordServiceImpl implements ReportRecordService {

    @Autowired
    private ReportRecordDao reportRecordDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private StudentDao studentDao;

    @Override
    public ReportRecord reportRecord(ReportRecord reportRecord) {
        ReportRecord record = null;
        if (reportRecord.getStuId() != null && !StringUtils.isEmpty(reportRecord.getStuId())) {
            Student student = studentDao.queryById(reportRecord.getStuId());
            record = student.getReportRecord();
            if (record == null) {
                // 插入记录
                reportRecord.setCreateTime(new Date());
                reportRecord.setUpdateTime(new Date());
                reportRecordDao.insert(reportRecord);
                record = reportRecord;
                // 建立外键关联与Student
                student.setReportRecord(record);
            } else {
                // 更新易变的信息 健康状态、体温、所在地
                record.setUpdateTime(new Date());
                record.setCurrentHealth(reportRecord.getCurrentHealth());
                record.setCurrentTemperature(reportRecord.getCurrentTemperature());
                record.setCurrentDistrict(reportRecord.getCurrentDistrict());
                reportRecordDao.update(record);
            }
            updateCount(record);
        }
        return record;
    }

    public Department getDeptReportRecords(Integer deptId) {
        Department department = null;
        if (deptId != null) {
            department = departmentDao.queryById(deptId);
        }
        return department;
    }

    @Override
    public Integer calcAbnormal(String deptName) {
        if (deptName != null) {
            return reportRecordDao.count(deptName);
        }
        return 0;
    }


    /**
     * 更新上报人数
     * 通过department的关联间接地更新班级的上报人数
     * @param record
     */
    private void updateCount(ReportRecord record) {
        Department department = departmentDao.queryByName(record.getDepartment());
        if (department != null) {
            department.setSubmittedCount(department.getSubmittedCount() + 1);
            Set<Grade> grades = department.getGrades();
            for (Grade grade : grades) {
                if (record.getInClass().equals(grade.getGradeName())) {
                    grade.setSubmittedCount(grade.getSubmittedCount() + 1);
                }
            }
            departmentDao.update(department);
        }
    }

    @Override
    public File exportRecords(Integer gradeId, String exportPath) {
        File file = null;
        List<Student> students = studentDao.queryReportIsNotNull(gradeId);
        List<ReportRecord> reportRecords = new ArrayList<>();
        if (students != null && students.size() > 0) {
            for (Student student : students) {
                student.getReportRecord();
                ReportRecord report = student.getReportRecord();
                report.setStuId(student.getStuId());
                reportRecords.add(report);
            }
        }
        if (reportRecords != null && reportRecords.size() > 0) {
            file = new File(exportPath + reportRecords.get(0).getInClass()+"登记信息表.xls");
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为sheet1 然后文件流会自动关闭
            EasyExcel.write(file, ReportRecord.class).sheet("sheet1").doWrite(reportRecords);
        }
        return file;
    }
}
